package com.mallotec.android.timenote.viewmodel;

import androidx.annotation.NonNull;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MediatorLiveData;
import androidx.lifecycle.ViewModel;

import com.mallotec.android.timenote.data.bean.Timenote;
import com.mallotec.android.timenote.data.repository.TimenoteRepository;

import java.util.List;

/**
 * @author reborn
 * @program TimeNote
 * @description
 * @create 2020-10-28 02:10
 **/
public class TimenoteListViewModel extends ViewModel {

    private TimenoteRepository timenoteRepository;
    // private MutableLiveData<Timenote> timenote;
    public LiveData<List<Timenote>> timenotesNotEnd;
    public LiveData<List<Timenote>> timenotesIsEnd;
    public MediatorLiveData<List<Timenote>> timenotesMerger;

    public boolean dataInited = false;

    public TimenoteListViewModel(@NonNull TimenoteRepository timenoteRepository) {
        super();
        this.timenoteRepository = timenoteRepository;
        // this.timenote = new MutableLiveData<>();
        // this.timenote.setValue(new Timenote());
        // this.timenotes = Transformations.switchMap(timenote, it -> this.timenoteRepository.getAllTimenotes());
        this.timenotesNotEnd = this.timenoteRepository.getTimenotesByEnd(false);
        this.timenotesIsEnd = this.timenoteRepository.getTimenotesByEnd(true);
        timenotesMerger = new MediatorLiveData<>();
        timenotesMerger.addSource(timenotesNotEnd, value -> timenotesMerger.setValue(value));
        timenotesMerger.addSource(timenotesIsEnd, value -> timenotesMerger.setValue(value));
    }

    /* public void setTimenote(MutableLiveData<Timenote> timenote) {
        this.timenote = timenote;
    }*/
    public void addTimenote(Timenote timenote) {
        timenoteRepository.insertTimenote(timenote);
    }
    public void updateTimenote(Timenote timenote) {
        timenoteRepository.updateTimenote(timenote);
    }
    public LiveData<Timenote> getTimenoteById(int timenoteId) {
        LiveData<Timenote> timenoteLiveData = timenoteRepository.findTimenoteById(timenoteId);
        return timenoteLiveData;
    }

}
